home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / NETFX.CAB / DefaultWsdlHelpGenerator.aspx < prev    next >
Encoding:
Text File  |  2003-02-21  |  60.3 KB  |  1,544 lines

  1. ∩╗┐<%@ Import Namespace="System.Collections" %>
  2. <%@ Import Namespace="System.IO" %>
  3. <%@ Import Namespace="System.Xml.Serialization" %>
  4. <%@ Import Namespace="System.Xml" %>
  5. <%@ Import Namespace="System.Xml.Schema" %>
  6. <%@ Import Namespace="System.Web.Services.Description" %>
  7. <%@ Import Namespace="System" %>
  8. <%@ Import Namespace="System.Globalization" %>
  9. <%@ Import Namespace="System.Resources" %>
  10. <%@ Import Namespace="System.Diagnostics" %>
  11. <%@ Import Namespace="System.Net" %>
  12.  
  13. <html>
  14.  
  15.     <script language="C#" runat="server">
  16.  
  17.     // set this to true if you want to see a POST test form instead of a GET test form
  18.     bool showPost = true;
  19.  
  20.     // set this to true if you want to see the raw XML as outputted from the XmlWriter (useful for debugging)
  21.     bool dontFilterXml = false;
  22.     
  23.     // set this higher or lower to adjust the depth into your object graph of the sample messages
  24.     int maxObjectGraphDepth = 4;
  25.  
  26.     // set this higher or lower to adjust the number of array items in sample messages
  27.     int maxArraySize = 2;
  28.  
  29.     // set this to true to see debug output in sample messages
  30.     bool debug = false;
  31.     
  32.     string soapNs = "http://schemas.xmlsoap.org/soap/envelope/";
  33.     string soapEncNs = "http://schemas.xmlsoap.org/soap/encoding/";
  34.     string msTypesNs = "http://microsoft.com/wsdl/types/";
  35.     string wsdlNs = "http://schemas.xmlsoap.org/wsdl/";
  36.  
  37.     string urType = "anyType";
  38.  
  39.     ServiceDescriptionCollection serviceDescriptions;
  40.     XmlSchemas schemas;
  41.     string operationName;
  42.     OperationBinding soapOperationBinding;
  43.     Operation soapOperation;
  44.     OperationBinding httpGetOperationBinding;
  45.     Operation httpGetOperation;
  46.     OperationBinding httpPostOperationBinding;
  47.     Operation httpPostOperation;
  48.     StringWriter writer;
  49.     MemoryStream xmlSrc;
  50.     XmlTextWriter xmlWriter;
  51.     Uri getUrl, postUrl;
  52.     Queue referencedTypes;
  53.     int hrefID;
  54.     bool operationExists = false;
  55.     int nextPrefix = 1;
  56.     static readonly char[] hexTable = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  57.     bool requestIsLocal = false;
  58.  
  59.     string OperationName {
  60.         get { return operationName; }
  61.     }
  62.  
  63.     string ServiceName { 
  64.         get { return serviceDescriptions[0].Services[0].Name; }
  65.     }
  66.  
  67.     string ServiceNamespace {
  68.         get { return serviceDescriptions[0].TargetNamespace; }
  69.     }
  70.  
  71.     string ServiceDocumentation { 
  72.         get { return serviceDescriptions[0].Services[0].Documentation; }
  73.     }
  74.  
  75.     string FileName {
  76.         get { 
  77.             return Path.GetFileName(Request.Path);
  78.         }
  79.     }
  80.  
  81.     string EscapedFileName {
  82.         get { 
  83.             return HttpUtility.UrlEncode(FileName, Encoding.UTF8);
  84.         }
  85.     }
  86.  
  87.     bool ShowingMethodList {
  88.         get { return operationName == null; }
  89.     }
  90.  
  91.     bool OperationExists {
  92.         get { return operationExists; }
  93.     }
  94.  
  95.     // encodes a Unicode string into utf-8 bytes then copies each byte into a new Unicode string,
  96.     // escaping bytes > 0x7f, per the URI escaping rules
  97.     static string UTF8EscapeString(string source) {
  98.         byte[] bytes = Encoding.UTF8.GetBytes(source);
  99.         StringBuilder sb = new StringBuilder(bytes.Length); // start out with enough room to hold each byte as a char
  100.         for (int i = 0; i < bytes.Length; i++) {
  101.             byte b = bytes[i];
  102.             if (b > 0x7f) {
  103.                 sb.Append('%');
  104.                 sb.Append(hexTable[(b >> 4) & 0x0f]);
  105.                 sb.Append(hexTable[(b     ) & 0x0f]);
  106.             }
  107.             else
  108.                 sb.Append((char)b);
  109.         }
  110.         return sb.ToString();
  111.     }
  112.  
  113.     string EscapeParam(string param) {
  114.         return HttpUtility.UrlEncode(param, Request.ContentEncoding);
  115.     }
  116.  
  117.     XmlQualifiedName XmlEscapeQName(XmlQualifiedName qname) {
  118.         return new XmlQualifiedName(XmlConvert.EncodeLocalName(qname.Name), qname.Namespace);
  119.     }
  120.  
  121.     string SoapOperationInput {
  122.         get { 
  123.             if (SoapOperationBinding == null) return "";
  124.             WriteBegin();
  125.  
  126.             Port port = FindPort(SoapOperationBinding.Binding);
  127.             SoapAddressBinding soapAddress = (SoapAddressBinding)port.Extensions.Find(typeof(SoapAddressBinding));
  128.  
  129.             string action = UTF8EscapeString(((SoapOperationBinding)SoapOperationBinding.Extensions.Find(typeof(SoapOperationBinding))).SoapAction);
  130.  
  131.             Uri uri = new Uri(soapAddress.Location, false);
  132.  
  133.             Write("POST ");
  134.             Write(uri.AbsolutePath);
  135.             Write(" HTTP/1.1");
  136.             WriteLine();
  137.  
  138.             Write("Host: ");
  139.             Write(uri.Host);
  140.             WriteLine();
  141.  
  142.             Write("Content-Type: text/xml; charset=utf-8");
  143.             WriteLine();
  144.  
  145.             Write("Content-Length: ");
  146.             WriteValue("length");
  147.             WriteLine();
  148.  
  149.             Write("SOAPAction: \"");
  150.             Write(action);
  151.             Write("\"");
  152.             WriteLine();
  153.  
  154.             WriteLine();
  155.             
  156.             WriteSoapMessage(SoapOperationBinding.Input, serviceDescriptions.GetMessage(SoapOperation.Messages.Input.Message));
  157.             return WriteEnd();
  158.         }
  159.     }
  160.  
  161.     string SoapOperationOutput {
  162.         get { 
  163.             if (SoapOperationBinding == null) return "";
  164.             WriteBegin();
  165.             if (SoapOperationBinding.Output == null) {
  166.                 Write("HTTP/1.1 202 Accepted");
  167.                 WriteLine();
  168.             }
  169.             else {
  170.                 Write("HTTP/1.1 200 OK");
  171.                 WriteLine();
  172.                 Write("Content-Type: text/xml; charset=utf-8");
  173.                 WriteLine();
  174.                 Write("Content-Length: ");
  175.                 WriteValue("length");
  176.                 WriteLine();
  177.                 WriteLine();
  178.  
  179.                 WriteSoapMessage(SoapOperationBinding.Output, serviceDescriptions.GetMessage(SoapOperation.Messages.Output.Message));
  180.             }
  181.             return WriteEnd();
  182.         }
  183.     }
  184.  
  185.     OperationBinding SoapOperationBinding {
  186.         get { 
  187.             if (soapOperationBinding == null)
  188.                 soapOperationBinding = FindBinding(typeof(SoapBinding));
  189.             return soapOperationBinding;
  190.         }
  191.     }
  192.  
  193.     Operation SoapOperation {
  194.         get { 
  195.             if (soapOperation == null)
  196.                 soapOperation = FindOperation(SoapOperationBinding);
  197.             return soapOperation;
  198.         }
  199.     }
  200.  
  201.     bool ShowingSoap {
  202.         get { 
  203.             return SoapOperationBinding != null; 
  204.         }
  205.     }
  206.  
  207.     private static string GetEncodedNamespace(string ns) {
  208.         if (ns.EndsWith("/"))
  209.             return ns + "encodedTypes";
  210.         else return ns + "/encodedTypes";
  211.     }
  212.  
  213.     void WriteSoapMessage(MessageBinding messageBinding, Message message) {
  214.         SoapOperationBinding soapBinding = (SoapOperationBinding)SoapOperationBinding.Extensions.Find(typeof(SoapOperationBinding));
  215.         SoapBodyBinding soapBodyBinding = (SoapBodyBinding)messageBinding.Extensions.Find(typeof(SoapBodyBinding));
  216.         bool rpc = soapBinding != null && soapBinding.Style == SoapBindingStyle.Rpc;
  217.         bool encoded = soapBodyBinding.Use == SoapBindingUse.Encoded;
  218.  
  219.         xmlWriter.WriteStartDocument();
  220.         xmlWriter.WriteStartElement("soap", "Envelope", soapNs);
  221.         DefineNamespace("xsi", XmlSchema.InstanceNamespace);
  222.         DefineNamespace("xsd", XmlSchema.Namespace);
  223.         if (encoded) {
  224.             DefineNamespace("soapenc", soapEncNs);
  225.             string targetNamespace = message.ServiceDescription.TargetNamespace;
  226.             DefineNamespace("tns", targetNamespace);
  227.             DefineNamespace("types", GetEncodedNamespace(targetNamespace));
  228.         }
  229.  
  230.         SoapHeaderBinding[] headers = (SoapHeaderBinding[])messageBinding.Extensions.FindAll(typeof(SoapHeaderBinding));
  231.         if (headers.Length > 0) {
  232.             xmlWriter.WriteStartElement("Header", soapNs);
  233.             foreach (SoapHeaderBinding header in headers) {
  234.                 Message headerMessage = serviceDescriptions.GetMessage(header.Message);
  235.                 if (headerMessage != null) {
  236.                     MessagePart part = headerMessage.Parts[header.Part];
  237.                     if (part != null) {
  238.                         if (encoded)
  239.                             WriteType(part.Type, part.Type, XmlSchemaForm.Qualified, -1, 0, false);
  240.                         else
  241.                             WriteTopLevelElement(XmlEscapeQName(part.Element), 0);
  242.                     }
  243.                 }
  244.             }
  245.             if (encoded)
  246.                 WriteQueuedTypes();
  247.             xmlWriter.WriteEndElement();
  248.         }
  249.  
  250.         xmlWriter.WriteStartElement("Body", soapNs);
  251.         if (soapBodyBinding.Encoding != null && soapBodyBinding.Encoding != String.Empty)
  252.             xmlWriter.WriteAttributeString("encodingStyle", soapNs, soapEncNs);
  253.  
  254.         if (rpc) {
  255.             string messageName = SoapOperationBinding.Output == messageBinding ? SoapOperation.Name + "Response" : SoapOperation.Name;
  256.             if (encoded) {
  257.                 string prefix = null;
  258.                 if (soapBodyBinding.Namespace.Length > 0) {
  259.                     prefix = xmlWriter.LookupPrefix(soapBodyBinding.Namespace);
  260.                     if (prefix == null)
  261.                         prefix = "q" + nextPrefix++;
  262.                 }
  263.                 xmlWriter.WriteStartElement(prefix, messageName, soapBodyBinding.Namespace);
  264.             }
  265.             else
  266.                 xmlWriter.WriteStartElement(messageName, soapBodyBinding.Namespace);
  267.         }
  268.         foreach (MessagePart part in message.Parts) {
  269.             if (encoded) {
  270.                 if (rpc)
  271.                     WriteType(new XmlQualifiedName(part.Name, soapBodyBinding.Namespace), part.Type, XmlSchemaForm.Unqualified, 0, 0, true);
  272.                 else
  273.                     WriteType(part.Type, part.Type, XmlSchemaForm.Qualified, -1, 0, true); // id == -1 writes the definition without writing the id attr
  274.             }
  275.             else {
  276.                 if (!part.Element.IsEmpty)
  277.                     WriteTopLevelElement(XmlEscapeQName(part.Element), 0);
  278.                 else
  279.                     WriteXmlValue("xml");
  280.             }
  281.         }
  282.         if (rpc) {
  283.             xmlWriter.WriteEndElement();
  284.         }
  285.         if (encoded) {
  286.             WriteQueuedTypes();
  287.         }            
  288.         xmlWriter.WriteEndElement();
  289.         xmlWriter.WriteEndElement();
  290.     }
  291.  
  292.     string HttpGetOperationInput {
  293.         get { 
  294.             if (TryGetUrl == null) return "";
  295.             WriteBegin();
  296.  
  297.             Write("GET ");
  298.             Write(TryGetUrl.AbsolutePath);
  299.  
  300.             Write("?");
  301.             WriteQueryStringMessage(serviceDescriptions.GetMessage(HttpGetOperation.Messages.Input.Message));
  302.             
  303.             Write(" HTTP/1.1");
  304.             WriteLine();
  305.  
  306.             Write("Host: ");
  307.             Write(TryGetUrl.Host);
  308.             WriteLine();
  309.  
  310.             return WriteEnd();
  311.         }
  312.     }
  313.  
  314.     string HttpGetOperationOutput {
  315.         get { 
  316.             if (HttpGetOperationBinding == null) return "";
  317.             if (HttpGetOperationBinding.Output == null) return "";
  318.             Message message = serviceDescriptions.GetMessage(HttpGetOperation.Messages.Output.Message);
  319.             WriteBegin();
  320.             Write("HTTP/1.1 200 OK");
  321.             WriteLine();
  322.             if (message.Parts.Count > 0) {
  323.                 Write("Content-Type: text/xml; charset=utf-8");
  324.                 WriteLine();
  325.                 Write("Content-Length: ");
  326.                 WriteValue("length");
  327.                 WriteLine();
  328.                 WriteLine();
  329.  
  330.                 WriteHttpReturnPart(message.Parts[0].Element);
  331.             }
  332.  
  333.             return WriteEnd();
  334.         }
  335.     }
  336.  
  337.     void WriteQueryStringMessage(Message message) {
  338.         bool first = true;
  339.         foreach (MessagePart part in message.Parts) {
  340.             int count = 1;
  341.             string typeName = part.Type.Name;
  342.             if (part.Type.Namespace != XmlSchema.Namespace && part.Type.Namespace != msTypesNs) {
  343.                 int arrIndex = typeName.IndexOf("Array");
  344.                 if (arrIndex >= 0) {
  345.                     typeName = CodeIdentifier.MakeCamel(typeName.Substring(0, arrIndex));
  346.                     count = 2;
  347.                 }
  348.             }
  349.             for (int i=0; i<count; i++) {
  350.                 if (first) {
  351.                     first = false; 
  352.                 }
  353.                 else {
  354.                     Write("&");
  355.                 }
  356.                 Write("<font class=key>");
  357.                 Write(part.Name);
  358.                 Write("</font>=");
  359.             
  360.                 WriteValue(typeName);
  361.             }
  362.         }
  363.     }
  364.  
  365.     OperationBinding HttpGetOperationBinding {
  366.         get { 
  367.             if (httpGetOperationBinding == null)
  368.                 httpGetOperationBinding = FindHttpBinding("GET");
  369.             return httpGetOperationBinding;
  370.         }
  371.     }
  372.  
  373.     Operation HttpGetOperation {
  374.         get { 
  375.             if (httpGetOperation == null)
  376.                 httpGetOperation = FindOperation(HttpGetOperationBinding);
  377.             return httpGetOperation;
  378.         }
  379.     }
  380.  
  381.     bool ShowingHttpGet {
  382.         get { 
  383.             return HttpGetOperationBinding != null; 
  384.         }
  385.     }
  386.  
  387.     string HttpPostOperationInput {
  388.         get { 
  389.             if (TryPostUrl == null) return "";
  390.             WriteBegin();
  391.  
  392.             Write("POST ");
  393.             Write(TryPostUrl.AbsolutePath);
  394.  
  395.             Write(" HTTP/1.1");
  396.             WriteLine();
  397.  
  398.             Write("Host: ");
  399.             Write(TryPostUrl.Host);
  400.             WriteLine();
  401.  
  402.             Write("Content-Type: application/x-www-form-urlencoded");
  403.             WriteLine();
  404.             Write("Content-Length: ");
  405.             WriteValue("length");
  406.             WriteLine();
  407.             WriteLine();
  408.  
  409.             WriteQueryStringMessage(serviceDescriptions.GetMessage(HttpPostOperation.Messages.Input.Message));
  410.  
  411.             return WriteEnd();
  412.         }
  413.     }
  414.  
  415.     string HttpPostOperationOutput {
  416.         get { 
  417.             if (HttpPostOperationBinding == null) return "";
  418.             if (HttpPostOperationBinding.Output == null) return "";
  419.             Message message = serviceDescriptions.GetMessage(HttpPostOperation.Messages.Output.Message);
  420.             WriteBegin();
  421.             Write("HTTP/1.1 200 OK");
  422.             WriteLine();
  423.             if (message.Parts.Count > 0) {
  424.                 Write("Content-Type: text/xml; charset=utf-8");
  425.                 WriteLine();
  426.                 Write("Content-Length: ");
  427.                 WriteValue("length");
  428.                 WriteLine();
  429.                 WriteLine();
  430.  
  431.                 WriteHttpReturnPart(message.Parts[0].Element);
  432.             }
  433.  
  434.             return WriteEnd();
  435.         }
  436.     }
  437.  
  438.     OperationBinding HttpPostOperationBinding {
  439.         get { 
  440.             if (httpPostOperationBinding == null)
  441.                 httpPostOperationBinding = FindHttpBinding("POST");
  442.             return httpPostOperationBinding;
  443.         }
  444.     }
  445.  
  446.     Operation HttpPostOperation {
  447.         get { 
  448.             if (httpPostOperation == null)
  449.                 httpPostOperation = FindOperation(HttpPostOperationBinding);
  450.             return httpPostOperation;
  451.         }
  452.     }
  453.  
  454.     bool ShowingHttpPost {
  455.         get { 
  456.             return HttpPostOperationBinding != null; 
  457.         }
  458.     }
  459.  
  460.     MessagePart[] TryGetMessageParts {
  461.         get { 
  462.             if (HttpGetOperationBinding == null) return new MessagePart[0];
  463.             Message message = serviceDescriptions.GetMessage(HttpGetOperation.Messages.Input.Message);
  464.             MessagePart[] parts = new MessagePart[message.Parts.Count];
  465.             message.Parts.CopyTo(parts, 0);
  466.             return parts;
  467.         }
  468.     }
  469.  
  470.     bool ShowGetTestForm {
  471.         get {
  472.             if (!ShowingHttpGet) return false;
  473.             Message message = serviceDescriptions.GetMessage(HttpGetOperation.Messages.Input.Message);
  474.             foreach (MessagePart part in message.Parts) {
  475.                 if (part.Type.Namespace != XmlSchema.Namespace && part.Type.Namespace != msTypesNs)
  476.                     return false;
  477.             }
  478.             return true;
  479.         }
  480.     }            
  481.  
  482.     Uri TryGetUrl {
  483.         get {
  484.             if (getUrl == null) {
  485.                 if (HttpGetOperationBinding == null) return null;
  486.                 Port port = FindPort(HttpGetOperationBinding.Binding);
  487.                 if (port == null) return null;
  488.                 HttpAddressBinding httpAddress = (HttpAddressBinding)port.Extensions.Find(typeof(HttpAddressBinding));
  489.                 HttpOperationBinding httpOperation = (HttpOperationBinding)HttpGetOperationBinding.Extensions.Find(typeof(HttpOperationBinding));
  490.                 if (httpAddress == null || httpOperation == null) return null;
  491.                 getUrl = new Uri(httpAddress.Location + httpOperation.Location);
  492.             }
  493.             return getUrl;
  494.         }
  495.     }
  496.  
  497.     MessagePart[] TryPostMessageParts {
  498.         get { 
  499.             if (HttpPostOperationBinding == null) return new MessagePart[0];
  500.             Message message = serviceDescriptions.GetMessage(HttpPostOperation.Messages.Input.Message);
  501.             MessagePart[] parts = new MessagePart[message.Parts.Count];
  502.             message.Parts.CopyTo(parts, 0);
  503.             return parts;
  504.         }
  505.     }
  506.  
  507.     bool ShowPostTestForm {
  508.         get {
  509.             if (!ShowingHttpPost) return false;
  510.             Message message = serviceDescriptions.GetMessage(HttpPostOperation.Messages.Input.Message);
  511.             foreach (MessagePart part in message.Parts) {
  512.                 if (part.Type.Namespace != XmlSchema.Namespace && part.Type.Namespace != msTypesNs)
  513.                     return false;
  514.             }
  515.             return true;
  516.         }
  517.     }            
  518.  
  519.     Uri TryPostUrl {
  520.         get {
  521.             if (postUrl == null) {
  522.                 if (HttpPostOperationBinding == null) return null;
  523.                 Port port = FindPort(HttpPostOperationBinding.Binding);
  524.                 if (port == null) return null;
  525.                 HttpAddressBinding httpAddress = (HttpAddressBinding)port.Extensions.Find(typeof(HttpAddressBinding));
  526.                 HttpOperationBinding httpOperation = (HttpOperationBinding)HttpPostOperationBinding.Extensions.Find(typeof(HttpOperationBinding));
  527.                 if (httpAddress == null || httpOperation == null) return null;
  528.                 postUrl = new Uri(httpAddress.Location + httpOperation.Location);
  529.             }
  530.             return postUrl;
  531.         }
  532.     }
  533.  
  534.     void WriteHttpReturnPart(XmlQualifiedName elementName) {
  535.         if (elementName == null || elementName.IsEmpty) {
  536.             Write("<?xml version=\"1.0\"?>");
  537.             WriteLine();
  538.             WriteValue("xml");
  539.         }
  540.         else {
  541.             xmlWriter.WriteStartDocument();
  542.             WriteTopLevelElement(XmlEscapeQName(elementName), 0);
  543.         }
  544.     }
  545.  
  546.     XmlSchemaType GetPartType(MessagePart part) {
  547.         if (part.Element != null && !part.Element.IsEmpty) {
  548.             XmlSchemaElement element = (XmlSchemaElement)schemas.Find(part.Element, typeof(XmlSchemaElement));
  549.             if (element != null) return element.SchemaType;
  550.             return null;
  551.         }
  552.         else if (part.Type != null && !part.Type.IsEmpty) {
  553.             XmlSchemaType xmlSchemaType = (XmlSchemaType)schemas.Find(part.Type, typeof(XmlSchemaSimpleType));
  554.             if (xmlSchemaType != null) return xmlSchemaType;
  555.             xmlSchemaType = (XmlSchemaType)schemas.Find(part.Type, typeof(XmlSchemaComplexType));
  556.             return xmlSchemaType;
  557.         }
  558.         return null;
  559.     }
  560.  
  561.     void WriteTopLevelElement(XmlQualifiedName name, int depth) {
  562.         WriteTopLevelElement((XmlSchemaElement)schemas.Find(name, typeof(XmlSchemaElement)), name.Namespace, depth);
  563.     }
  564.  
  565.     void WriteTopLevelElement(XmlSchemaElement element, string ns, int depth) {
  566.         WriteElement(element, ns, XmlSchemaForm.Qualified, false, 0, depth, false);
  567.     }
  568.  
  569.     class QueuedType {
  570.         internal XmlQualifiedName name;
  571.         internal XmlQualifiedName typeName;
  572.         internal XmlSchemaForm form;
  573.         internal int id;
  574.         internal int depth;
  575.         internal bool writeXsiType;
  576.     }
  577.  
  578.     void WriteQueuedTypes() {
  579.         while (referencedTypes.Count > 0) {
  580.             QueuedType q = (QueuedType)referencedTypes.Dequeue();
  581.             WriteType(q.name, q.typeName, q.form, q.id, q.depth, q.writeXsiType);
  582.         }
  583.     }
  584.  
  585.     void AddQueuedType(XmlQualifiedName name, XmlQualifiedName type, int id, int depth, bool writeXsiType) {
  586.         AddQueuedType(name, type, XmlSchemaForm.Unqualified, id, depth, writeXsiType);
  587.     }
  588.  
  589.     void AddQueuedType(XmlQualifiedName name, XmlQualifiedName typeName, XmlSchemaForm form, int id, int depth, bool writeXsiType) {
  590.         QueuedType q = new QueuedType();
  591.         q.name = name;
  592.         q.typeName = typeName;
  593.         q.form = form;
  594.         q.id = id;
  595.         q.depth = depth;
  596.         q.writeXsiType = writeXsiType;
  597.         referencedTypes.Enqueue(q);
  598.     }
  599.  
  600.     void WriteType(XmlQualifiedName name, XmlQualifiedName typeName, int id, int depth, bool writeXsiType) {
  601.         WriteType(name, typeName, XmlSchemaForm.None, id, depth, writeXsiType);
  602.     }
  603.  
  604.     void WriteType(XmlQualifiedName name, XmlQualifiedName typeName, XmlSchemaForm form, int id, int depth, bool writeXsiType) {
  605.         XmlSchemaElement element = new XmlSchemaElement();
  606.         element.Name = name.Name;
  607.         element.MaxOccurs = 1;
  608.         element.Form = form;
  609.         element.SchemaTypeName = typeName;
  610.         WriteElement(element, name.Namespace, true, id, depth, writeXsiType);
  611.     }
  612.  
  613.     void WriteElement(XmlSchemaElement element, string ns, bool encoded, int id, int depth, bool writeXsiType) {
  614.         XmlSchemaForm form = element.Form;
  615.         if (form == XmlSchemaForm.None) {
  616.             XmlSchema schema = schemas[ns];
  617.             if (schema != null) form = schema.ElementFormDefault;
  618.         }
  619.         WriteElement(element, ns, form, encoded, id, depth, writeXsiType);
  620.     }
  621.  
  622.     void WriteElement(XmlSchemaElement element, string ns, XmlSchemaForm form, bool encoded, int id, int depth, bool writeXsiType) {
  623.         if (element == null) return;
  624.         int count = element.MaxOccurs > 1 ? maxArraySize : 1;
  625.  
  626.         for (int i = 0; i < count; i++) {
  627.             XmlQualifiedName elementName = (element.QualifiedName == null || element.QualifiedName.IsEmpty ? new XmlQualifiedName(element.Name, ns) : element.QualifiedName);
  628.             if (encoded && count > 1) {
  629.                 elementName = new XmlQualifiedName("Item", null);
  630.             }
  631.  
  632.             if (IsRef(element.RefName)) {
  633.                 WriteTopLevelElement(XmlEscapeQName(element.RefName), depth);
  634.                 continue;
  635.             }
  636.             
  637.             if (encoded) {
  638.                 string prefix = null;
  639.                 if (form != XmlSchemaForm.Unqualified && elementName.Namespace.Length > 0) {
  640.                     prefix = xmlWriter.LookupPrefix(elementName.Namespace);
  641.                     if (prefix == null)
  642.                         prefix = "q" + nextPrefix++;
  643.                 }
  644.                 
  645.                 if (id != 0) { // intercept array definitions
  646.                     XmlSchemaComplexType ct = null;
  647.                     if (IsStruct(element, out ct)) {
  648.                         XmlQualifiedName typeName = element.SchemaTypeName;
  649.                         XmlQualifiedName baseTypeName = GetBaseTypeName(ct);
  650.                         if (baseTypeName != null && IsArray(baseTypeName))
  651.                             typeName = baseTypeName;
  652.                         if (typeName != elementName) {
  653.                             WriteType(typeName, element.SchemaTypeName, form, id, depth, writeXsiType);
  654.                             return;
  655.                         }
  656.                     }
  657.                 }
  658.                 xmlWriter.WriteStartElement(prefix, elementName.Name, form != XmlSchemaForm.Unqualified ? elementName.Namespace : "");
  659.             }
  660.             else
  661.                 xmlWriter.WriteStartElement(elementName.Name, form != XmlSchemaForm.Unqualified ? elementName.Namespace : "");
  662.  
  663.             XmlSchemaSimpleType simpleType = null;
  664.             XmlSchemaComplexType complexType = null;
  665.  
  666.             if (IsPrimitive(element.SchemaTypeName)) {
  667.                 if (writeXsiType) WriteTypeAttribute(element.SchemaTypeName);
  668.                 WritePrimitive(element.SchemaTypeName);
  669.             }
  670.             else if (IsEnum(element.SchemaTypeName, out simpleType)) {
  671.                 if (writeXsiType) WriteTypeAttribute(element.SchemaTypeName);
  672.                 WriteEnum(simpleType);
  673.             }
  674.             else if (IsStruct(element, out complexType)) {
  675.                 if (depth >= maxObjectGraphDepth)
  676.                     WriteNullAttribute(encoded);
  677.                 else if (encoded) {
  678.                     if (id != 0) {
  679.                         // id == -1 means write the definition without writing the id
  680.                         if (id > 0) {
  681.                             WriteIDAttribute(id);
  682.                         }
  683.                         WriteComplexType(complexType, ns, encoded, depth, writeXsiType);
  684.                     }
  685.                     else {
  686.                         int href = hrefID++;
  687.                         WriteHref(href);
  688.                         AddQueuedType(elementName, element.SchemaTypeName, XmlSchemaForm.Qualified, href, depth, true);
  689.                     }
  690.                 }
  691.                 else {
  692.                     WriteComplexType(complexType, ns, encoded, depth, false);
  693.                 }
  694.             }
  695.             else if (IsByteArray(element, out simpleType)) {
  696.                 WriteByteArray(simpleType);
  697.             }
  698.             else if (IsSchemaRef(element.RefName)) {
  699.                 WriteXmlValue("schema");
  700.             }
  701.             else if (IsUrType(element.SchemaTypeName)) {
  702.                 WriteTypeAttribute(new XmlQualifiedName(GetXmlValue("type"), null));
  703.             }
  704.             else {
  705.                 if (debug) {
  706.                     WriteDebugAttribute("error", "Unknown type");
  707.                     WriteDebugAttribute("elementName", element.QualifiedName.ToString());
  708.                     WriteDebugAttribute("typeName", element.SchemaTypeName.ToString());
  709.                     WriteDebugAttribute("type", element.SchemaType != null ? element.SchemaType.ToString() : "null");
  710.                 }
  711.             }
  712.             xmlWriter.WriteEndElement();
  713.             xmlWriter.Formatting = Formatting.Indented;
  714.         }
  715.     }
  716.  
  717.     bool IsArray(XmlQualifiedName typeName) {
  718.         return (typeName.Namespace == soapEncNs && typeName.Name == "Array");
  719.     }
  720.  
  721.     bool IsPrimitive(XmlQualifiedName typeName) {
  722.         return (!typeName.IsEmpty && 
  723.                 (typeName.Namespace == XmlSchema.Namespace || typeName.Namespace == msTypesNs) &&
  724.                 typeName.Name != urType);
  725.     }
  726.  
  727.     bool IsRef(XmlQualifiedName refName) {
  728.         return refName != null && !refName.IsEmpty && !IsSchemaRef(refName);
  729.     }
  730.  
  731.     bool IsSchemaRef(XmlQualifiedName refName) {
  732.         return refName != null && refName.Name == "schema" && refName.Namespace == XmlSchema.Namespace;
  733.     }
  734.  
  735.     bool IsUrType(XmlQualifiedName typeName) {
  736.         return (!typeName.IsEmpty && typeName.Namespace == XmlSchema.Namespace && typeName.Name == urType);
  737.     }
  738.  
  739.     bool IsEnum(XmlQualifiedName typeName, out XmlSchemaSimpleType type) {
  740.         XmlSchemaSimpleType simpleType = null;
  741.         if (typeName != null && !typeName.IsEmpty) {
  742.             simpleType = (XmlSchemaSimpleType)schemas.Find(typeName, typeof(XmlSchemaSimpleType));
  743.             if (simpleType != null) {
  744.                 type = simpleType;
  745.                 return true;
  746.             }
  747.         }
  748.         type = null;
  749.         return false;
  750.     }
  751.  
  752.     bool IsStruct(XmlSchemaElement element, out XmlSchemaComplexType type) {
  753.         XmlSchemaComplexType complexType = null;
  754.  
  755.         if (!element.SchemaTypeName.IsEmpty) {
  756.             complexType = (XmlSchemaComplexType)schemas.Find(element.SchemaTypeName, typeof(XmlSchemaComplexType));
  757.             if (complexType != null) {
  758.                 type = complexType;
  759.                 return true;
  760.             }
  761.         }
  762.         if (element.SchemaType != null && element.SchemaType is XmlSchemaComplexType) {
  763.             complexType = element.SchemaType as XmlSchemaComplexType;
  764.             if (complexType != null) {
  765.                 type = complexType;
  766.                 return true;
  767.             }
  768.         }
  769.         type = null;
  770.         return false;
  771.     }
  772.  
  773.     bool IsByteArray(XmlSchemaElement element, out XmlSchemaSimpleType type) {
  774.         if (element.SchemaTypeName.IsEmpty && element.SchemaType is XmlSchemaSimpleType) {
  775.             type = element.SchemaType as XmlSchemaSimpleType;
  776.             return true;
  777.         }
  778.         type = null;
  779.         return false;
  780.     }
  781.     
  782.     XmlQualifiedName ArrayItemType(string typeDef) {
  783.         string ns;
  784.         string name;
  785.  
  786.         int nsLen = typeDef.LastIndexOf(':');
  787.  
  788.         if (nsLen <= 0) {
  789.             ns = "";
  790.         }
  791.         else {
  792.             ns = typeDef.Substring(0, nsLen);
  793.         }
  794.         int nameLen = typeDef.IndexOf('[', nsLen + 1);
  795.  
  796.         if (nameLen <= nsLen) {
  797.             return new XmlQualifiedName(urType, XmlSchema.Namespace);
  798.         }
  799.         name = typeDef.Substring(nsLen + 1, nameLen - nsLen - 1);
  800.  
  801.         return new XmlQualifiedName(name, ns);
  802.     }
  803.  
  804.     void WriteByteArray(XmlSchemaSimpleType dataType) {
  805.         WriteXmlValue("bytes");
  806.     }
  807.  
  808.     void WriteEnum(XmlSchemaSimpleType dataType) {
  809.         if (dataType.Content is XmlSchemaSimpleTypeList) { // "flags" enum -- appears inside a list
  810.             XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)dataType.Content;
  811.             dataType = list.ItemType;
  812.         }
  813.  
  814.         bool first = true;
  815.         if (dataType.Content is XmlSchemaSimpleTypeRestriction) {
  816.             XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
  817.             foreach (XmlSchemaFacet facet in restriction.Facets) {
  818.                 if (facet is XmlSchemaEnumerationFacet) {
  819.                     if (!first) xmlWriter.WriteString(" or "); else first = false;
  820.                     WriteXmlValue(facet.Value);
  821.                 }
  822.             }
  823.         }
  824.     }
  825.  
  826.     void WriteArrayTypeAttribute(XmlQualifiedName type, int maxOccurs) {
  827.         StringBuilder sb = new StringBuilder(type.Name);
  828.         sb.Append("[");
  829.         sb.Append(maxOccurs.ToString());
  830.         sb.Append("]");
  831.         string prefix = DefineNamespace("q1", type.Namespace);
  832.         XmlQualifiedName typeName = new XmlQualifiedName(sb.ToString(), prefix);
  833.         xmlWriter.WriteAttributeString("arrayType", soapEncNs, typeName.ToString());
  834.     }
  835.  
  836.     void WriteTypeAttribute(XmlQualifiedName type) {
  837.         string prefix = DefineNamespace("s0", type.Namespace);
  838.         xmlWriter.WriteStartAttribute("type", XmlSchema.InstanceNamespace);
  839.         xmlWriter.WriteString(new XmlQualifiedName(type.Name, prefix).ToString());
  840.         xmlWriter.WriteEndAttribute();
  841.     }
  842.  
  843.     void WriteNullAttribute(bool encoded) {
  844.         if (encoded)
  845.             xmlWriter.WriteAttributeString("null", XmlSchema.InstanceNamespace, "1");
  846.         else
  847.             xmlWriter.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
  848.     }
  849.  
  850.     void WriteIDAttribute(int href) {
  851.         xmlWriter.WriteAttributeString("id", "id" + href.ToString());
  852.     }
  853.  
  854.     void WriteHref(int href) {
  855.         xmlWriter.WriteAttributeString("href", "#id" + href.ToString());
  856.     }
  857.  
  858.     void WritePrimitive(XmlQualifiedName name) {
  859.         if (name.Namespace == XmlSchema.Namespace && name.Name == "QName") {
  860.             DefineNamespace("q1", "http://tempuri.org/SampleNamespace");
  861.             WriteXmlValue("q1:QName");
  862.         }
  863.         else
  864.             WriteXmlValue(name.Name);
  865.     }
  866.         
  867.     XmlQualifiedName GetBaseTypeName(XmlSchemaComplexType complexType) {
  868.         if (complexType.ContentModel is XmlSchemaComplexContent) {
  869.             XmlSchemaComplexContent content = (XmlSchemaComplexContent)complexType.ContentModel;
  870.             if (content.Content is XmlSchemaComplexContentRestriction) {
  871.                 XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)content.Content;
  872.                 return restriction.BaseTypeName;
  873.             }
  874.         }
  875.         return null;
  876.     }
  877.  
  878.     internal class TypeItems {
  879.         internal XmlSchemaObjectCollection Attributes = new XmlSchemaObjectCollection();
  880.         internal XmlSchemaAnyAttribute AnyAttribute;
  881.         internal XmlSchemaObjectCollection Items = new XmlSchemaObjectCollection();
  882.         internal XmlQualifiedName baseSimpleType;
  883.     }
  884.  
  885.     TypeItems GetTypeItems(XmlSchemaComplexType type) {
  886.         TypeItems items = new TypeItems();
  887.         if (type == null)
  888.             return items;
  889.  
  890.         XmlSchemaParticle particle = null;
  891.         if (type.ContentModel != null) {
  892.             XmlSchemaContent content = type.ContentModel.Content;
  893.             if (content is XmlSchemaComplexContentExtension) {
  894.                 XmlSchemaComplexContentExtension extension = (XmlSchemaComplexContentExtension)content;
  895.                 items.Attributes = extension.Attributes;
  896.                 items.AnyAttribute = extension.AnyAttribute;
  897.                 particle = extension.Particle;
  898.             }
  899.             else if (content is XmlSchemaComplexContentRestriction) {
  900.                 XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)content;
  901.                 items.Attributes = restriction.Attributes;
  902.                 items.AnyAttribute = restriction.AnyAttribute;
  903.                 particle = restriction.Particle;
  904.             }
  905.             else if (content is XmlSchemaSimpleContentExtension) {
  906.                 XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension)content;
  907.                 items.Attributes = extension.Attributes;
  908.                 items.AnyAttribute = extension.AnyAttribute;
  909.                 items.baseSimpleType = extension.BaseTypeName;
  910.             }
  911.             else if (content is XmlSchemaSimpleContentRestriction) {
  912.                 XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction)content;
  913.                 items.Attributes = restriction.Attributes;
  914.                 items.AnyAttribute = restriction.AnyAttribute;
  915.                 items.baseSimpleType = restriction.BaseTypeName;
  916.             }
  917.         }
  918.         else {
  919.             items.Attributes = type.Attributes;
  920.             items.AnyAttribute = type.AnyAttribute;
  921.             particle = type.Particle;
  922.         }
  923.         if (particle != null) {
  924.             if (particle is XmlSchemaGroupRef) {
  925.                 XmlSchemaGroupRef refGroup = (XmlSchemaGroupRef)particle;
  926.  
  927.                 XmlSchemaGroup group = (XmlSchemaGroup)schemas.Find(refGroup.RefName, typeof(XmlSchemaGroup));
  928.                 if (group != null) {
  929.                     items.Items = group.Particle.Items;
  930.                 }
  931.             }
  932.             else if (particle is XmlSchemaGroupBase) {
  933.                 items.Items = ((XmlSchemaGroupBase)particle).Items;
  934.             }
  935.         }
  936.         return items;
  937.     }
  938.  
  939.     void WriteComplexType(XmlSchemaComplexType type, string ns, bool encoded, int depth, bool writeXsiType) {
  940.         bool wroteArrayType = false;
  941.         bool isSoapArray = false;
  942.         TypeItems typeItems = GetTypeItems(type);
  943.         if (encoded) {
  944.             /*
  945.               Check to see if the type looks like the new WSDL 1.1 array delaration:
  946.  
  947.               <xsd:complexType name="ArrayOfInt">
  948.                 <xsd:complexContent mixed="false">
  949.                   <xsd:restriction base="soapenc:Array">
  950.                     <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:int[]" />
  951.                   </xsd:restriction>
  952.                 </xsd:complexContent>
  953.               </xsd:complexType>
  954.  
  955.             */
  956.  
  957.             XmlQualifiedName itemType = null;
  958.             XmlQualifiedName topItemType = null;
  959.             string brackets = "";
  960.             XmlSchemaComplexType t = type;
  961.             XmlQualifiedName baseTypeName = GetBaseTypeName(t);
  962.             TypeItems arrayItems = typeItems;
  963.             while (t != null) {
  964.                 XmlSchemaObjectCollection attributes = arrayItems.Attributes;
  965.                 t = null; // if we don't set t after this stop looping
  966.                 if (baseTypeName != null && IsArray(baseTypeName) && attributes.Count > 0) {
  967.                     XmlSchemaAttribute refAttr = attributes[0] as XmlSchemaAttribute;
  968.                     if (refAttr != null) {
  969.                         XmlQualifiedName qnameArray = refAttr.RefName;
  970.                         if (qnameArray.Namespace == soapEncNs && qnameArray.Name == "arrayType") {
  971.                             isSoapArray = true;
  972.                             XmlAttribute typeAttribute = refAttr.UnhandledAttributes[0];
  973.                             if (typeAttribute.NamespaceURI == wsdlNs && typeAttribute.LocalName == "arrayType") {
  974.                                 itemType = ArrayItemType(typeAttribute.Value);
  975.                                 if (topItemType == null)
  976.                                     topItemType = itemType;
  977.                                 else
  978.                                     brackets += "[]";
  979.  
  980.                                 if (!IsPrimitive(itemType)) {
  981.                                     t = (XmlSchemaComplexType)schemas.Find(itemType, typeof(XmlSchemaComplexType));
  982.                                     arrayItems = GetTypeItems(t);
  983.                                 }
  984.                             }
  985.                         }
  986.                     }
  987.                 }
  988.             }
  989.             if (itemType != null) {
  990.                 wroteArrayType = true;
  991.                 if (IsUrType(itemType))
  992.                     WriteArrayTypeAttribute(new XmlQualifiedName(GetXmlValue("type") + brackets, null), maxArraySize);
  993.                 else
  994.                     WriteArrayTypeAttribute(new XmlQualifiedName(itemType.Name + brackets, itemType.Namespace), maxArraySize);
  995.                 
  996.                 for (int i = 0; i < maxArraySize; i++) {
  997.                     WriteType(new XmlQualifiedName("Item", null), topItemType, 0, depth+1, false);
  998.                 }
  999.             }
  1000.         }
  1001.  
  1002.         if (writeXsiType && !wroteArrayType) {
  1003.             WriteTypeAttribute(type.QualifiedName);
  1004.         }
  1005.  
  1006.         if (!isSoapArray) {
  1007.             foreach (XmlSchemaAttribute attr in typeItems.Attributes) {
  1008.                 if (attr != null && attr.Use != XmlSchemaUse.Prohibited) {
  1009.                     if (attr.Form == XmlSchemaForm.Qualified && attr.QualifiedName != null)
  1010.                         xmlWriter.WriteStartAttribute(attr.Name, attr.QualifiedName.Namespace);
  1011.                     else
  1012.                         xmlWriter.WriteStartAttribute(attr.Name, null);
  1013.  
  1014.                     XmlSchemaSimpleType dataType = null;
  1015.  
  1016.                     // special code for the QNames
  1017.                     if (attr.SchemaTypeName.Namespace == XmlSchema.Namespace && attr.SchemaTypeName.Name == "QName") {
  1018.                         WriteXmlValue("q1:QName");
  1019.                         xmlWriter.WriteEndAttribute();
  1020.                         DefineNamespace("q1", "http://tempuri.org/SampleNamespace");
  1021.                     }
  1022.                     else {
  1023.                         if (IsPrimitive(attr.SchemaTypeName)) 
  1024.                             WriteXmlValue(attr.SchemaTypeName.Name);
  1025.                         else if (IsEnum(attr.SchemaTypeName, out dataType))
  1026.                             WriteEnum(dataType);
  1027.                         xmlWriter.WriteEndAttribute();
  1028.                     }
  1029.                 }
  1030.             }
  1031.         }
  1032.  
  1033.         XmlSchemaObjectCollection items = typeItems.Items;
  1034.         foreach (object item in items) {
  1035.             if (item is XmlSchemaElement) {
  1036.                 WriteElement((XmlSchemaElement)item, ns, encoded, 0, depth + 1, encoded);
  1037.             }
  1038.             else if (item is XmlSchemaAny) {
  1039.                 XmlSchemaAny any = (XmlSchemaAny)item;
  1040.                 XmlSchema schema = schemas[any.Namespace];
  1041.                 if (schema == null) {
  1042.                     WriteXmlValue("xml");
  1043.                 }
  1044.                 else {
  1045.                     foreach (object schemaItem in schema.Items) {
  1046.                         if (schemaItem is XmlSchemaElement) {
  1047.                             if (IsDataSetRoot((XmlSchemaElement)schemaItem))
  1048.                                 WriteXmlValue("dataset");
  1049.                             else
  1050.                                 WriteTopLevelElement((XmlSchemaElement)schemaItem, any.Namespace, depth + 1);
  1051.                         }
  1052.                     }
  1053.                 }
  1054.             }
  1055.         }
  1056.     }
  1057.  
  1058.     bool IsDataSetRoot(XmlSchemaElement element) {
  1059.         if (element.UnhandledAttributes == null) return false;
  1060.         foreach (XmlAttribute a in element.UnhandledAttributes) {
  1061.             if (a.NamespaceURI == "urn:schemas-microsoft-com:xml-msdata" && a.LocalName == "IsDataSet")
  1062.                 return true;
  1063.         }
  1064.         return false;
  1065.     }
  1066.  
  1067.     void WriteBegin() {
  1068.         writer = new StringWriter();
  1069.         xmlSrc = new MemoryStream();
  1070.         xmlWriter = new XmlTextWriter(xmlSrc, new UTF8Encoding(false));
  1071.         xmlWriter.Formatting = Formatting.Indented;
  1072.         xmlWriter.Indentation = 2;
  1073.         referencedTypes = new Queue();
  1074.         hrefID = 1;
  1075.     }
  1076.  
  1077.     string WriteEnd() {
  1078.         xmlWriter.Flush();
  1079.         xmlSrc.Position = 0;
  1080.         StreamReader reader = new StreamReader(xmlSrc, Encoding.UTF8);
  1081.         writer.Write(HtmlEncode(reader.ReadToEnd()));
  1082.         return writer.ToString();
  1083.     }
  1084.  
  1085.     string HtmlEncode(string text) {
  1086.         StringBuilder sb = new StringBuilder();
  1087.         for (int i=0; i<text.Length; i++) {
  1088.             char c = text[i];
  1089.             if (c == '&') {
  1090.                 string special = ReadComment(text, i);
  1091.                 if (special.Length > 0) {
  1092.                     sb.Append(Server.HtmlDecode(special));
  1093.                     i += (special.Length + "<!--".Length + "-->".Length - 1);
  1094.                 }
  1095.                 else
  1096.                     sb.Append("&");
  1097.             }
  1098.             else if (c == '<')
  1099.                 sb.Append("<");
  1100.             else if (c == '>')
  1101.                 sb.Append(">");
  1102.             else
  1103.                 sb.Append(c);
  1104.         }
  1105.         return sb.ToString();
  1106.     }
  1107.  
  1108.     string ReadComment(string text, int index) {
  1109.         if (dontFilterXml) return String.Empty;
  1110.         if (String.Compare(text, index, "<!--", 0, "<!--".Length, false, CultureInfo.InvariantCulture) == 0) {
  1111.             int start = index + "<!--".Length;
  1112.             int end = text.IndexOf("-->", start);
  1113.             if (end < 0) return String.Empty;
  1114.             return text.Substring(start, end-start);
  1115.         }
  1116.         return String.Empty;
  1117.     }
  1118.  
  1119.     void Write(string text) {
  1120.         writer.Write(text);
  1121.     }
  1122.  
  1123.     void WriteLine() {
  1124.         writer.WriteLine();
  1125.     }
  1126.  
  1127.     void WriteValue(string text) {
  1128.         Write("<font class=value>" + text + "</font>");
  1129.     }
  1130.  
  1131.     void WriteStartXmlValue() {
  1132.         xmlWriter.WriteString("<!--<font class=value>");
  1133.     }
  1134.  
  1135.     void WriteEndXmlValue() {
  1136.         xmlWriter.WriteString("</font>-->");
  1137.     }
  1138.  
  1139.     void WriteDebugAttribute(string text) {
  1140.         WriteDebugAttribute("debug", text);
  1141.     }
  1142.  
  1143.     void WriteDebugAttribute(string id, string text) {
  1144.         xmlWriter.WriteAttributeString(id, text);
  1145.     }
  1146.  
  1147.     string GetXmlValue(string text) {
  1148.         return "<!--<font class=value>" + text + "</font>-->";
  1149.     }
  1150.  
  1151.     void WriteXmlValue(string text) {
  1152.         xmlWriter.WriteString(GetXmlValue(text));
  1153.     }
  1154.  
  1155.     string DefineNamespace(string prefix, string ns) {
  1156.         if (ns == null || ns == String.Empty) return null;
  1157.         string existingPrefix = xmlWriter.LookupPrefix(ns);
  1158.         if (existingPrefix != null && existingPrefix.Length > 0)
  1159.             return existingPrefix;
  1160.         xmlWriter.WriteAttributeString("xmlns", prefix, null, ns);
  1161.         return prefix;
  1162.     }
  1163.  
  1164.     Port FindPort(Binding binding) {
  1165.         foreach (ServiceDescription description in serviceDescriptions) {
  1166.             foreach (Service service in description.Services) {
  1167.                 foreach (Port port in service.Ports) {
  1168.                     if (port.Binding.Name == binding.Name &&
  1169.                         port.Binding.Namespace == binding.ServiceDescription.TargetNamespace) {
  1170.                         return port;
  1171.                     }
  1172.                 }
  1173.             }
  1174.         }
  1175.         return null;
  1176.     }
  1177.  
  1178.     OperationBinding FindBinding(Type bindingType) {
  1179.         OperationBinding nextBestMatch = null;
  1180.         foreach (ServiceDescription description in serviceDescriptions) {
  1181.             foreach (Binding binding in description.Bindings) {
  1182.                 object ext = binding.Extensions.Find(bindingType);
  1183.                 if (ext == null) continue;
  1184.                 foreach (OperationBinding operationBinding in binding.Operations) {
  1185.                     string messageName = operationBinding.Input.Name;
  1186.                     if (messageName == null || messageName.Length == 0) 
  1187.                         messageName = operationBinding.Name;
  1188.                     if (messageName == operationName) {
  1189.                         if (ext.GetType() != bindingType) {
  1190.                             nextBestMatch = operationBinding; // see if we can do better
  1191.                             break;
  1192.                         }
  1193.                         else
  1194.                             return operationBinding;
  1195.                     }
  1196.                 }
  1197.             }
  1198.         }
  1199.         return nextBestMatch;
  1200.     }
  1201.  
  1202.     OperationBinding FindHttpBinding(string verb) {
  1203.         foreach (ServiceDescription description in serviceDescriptions) {
  1204.             foreach (Binding binding in description.Bindings) {
  1205.                 HttpBinding httpBinding = (HttpBinding)binding.Extensions.Find(typeof(HttpBinding));
  1206.                 if (httpBinding == null) 
  1207.                     continue;
  1208.                 if (httpBinding.Verb != verb) 
  1209.                     continue;
  1210.                 foreach (OperationBinding operationBinding in binding.Operations) {
  1211.                     string messageName = operationBinding.Input.Name;
  1212.                     if (messageName == null || messageName.Length == 0) 
  1213.                         messageName = operationBinding.Name;
  1214.                     if (messageName == operationName) 
  1215.                         return operationBinding;
  1216.                 }
  1217.             }
  1218.         }
  1219.         return null;
  1220.     }
  1221.  
  1222.     Operation FindOperation(OperationBinding operationBinding) {
  1223.         PortType portType = serviceDescriptions.GetPortType(operationBinding.Binding.Type);
  1224.         foreach (Operation operation in portType.Operations) {
  1225.             if (operation.IsBoundBy(operationBinding)) {
  1226.                 return operation;
  1227.             }
  1228.         }
  1229.         return null;
  1230.     }
  1231.  
  1232.     string GetLocalizedText(string name) {
  1233.       return GetLocalizedText(name, new object[0]);
  1234.     }
  1235.  
  1236.     string GetLocalizedText(string name, object[] args) {
  1237.       ResourceManager rm = (ResourceManager)Application["RM"];
  1238.       string val = rm.GetString("HelpGenerator" + name);
  1239.       if (val == null) return String.Empty;
  1240.       return String.Format(val, args);
  1241.     }
  1242.  
  1243.     void Page_Load(object sender, EventArgs e) {
  1244.         if (Application["RM"] == null) {
  1245.             lock (this.GetType()) {
  1246.                 if (Application["RM"] == null) {
  1247.                     Application["RM"] = new ResourceManager("System.Web.Services", typeof(System.Web.Services.WebService).Assembly);
  1248.                 }
  1249.             }
  1250.         }
  1251.  
  1252.         operationName = Request.QueryString["op"];
  1253.  
  1254.         // Slots filled on HttpContext:
  1255.         // "wsdls"              A ServiceDescriptionCollection representing what is displayed for .asmx?wsdl
  1256.         // "schemas"            An XmlSchemas object containing schemas associated with .asmx?wsdl
  1257.         // "wsdlsWithPost"      Wsdls the same as "wsdls", plus bindings for the HttpPost protocol.
  1258.         // "schemasWithPost"    Schemas corresponding to "wsdlsWithPost".
  1259.         // The objects stored at "wsdlsWithPost" and "schemasWithPost" are available if
  1260.         // the HttpPost protocol is turned on in config.
  1261.         
  1262.         // Obtain WSDL contract from Http Context
  1263.  
  1264.         XmlSchemas schemasToUse;
  1265.         serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdlsWithPost"];
  1266.         if (serviceDescriptions != null) {
  1267.             requestIsLocal = true;
  1268.             schemasToUse = (XmlSchemas) Context.Items["schemasWithPost"];
  1269.         }
  1270.         else {
  1271.             serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
  1272.             schemasToUse = (XmlSchemas) Context.Items["schemas"];
  1273.         }
  1274.  
  1275.         schemas = new XmlSchemas();
  1276.         foreach (XmlSchema schema in schemasToUse) {
  1277.             schemas.Add(schema);
  1278.         }
  1279.         foreach (ServiceDescription description in serviceDescriptions) {
  1280.             foreach (XmlSchema schema in description.Types.Schemas) {
  1281.                 schemas.Add(schema);
  1282.             }
  1283.         }
  1284.  
  1285.         Hashtable methodsTable = new Hashtable();
  1286.  
  1287.         operationExists = false;
  1288.         foreach (ServiceDescription description in serviceDescriptions) {
  1289.             foreach (PortType portType in description.PortTypes) {
  1290.                 foreach (Operation operation in portType.Operations) {
  1291.                     string messageName = operation.Messages.Input.Name;
  1292.                     if (messageName == null || messageName.Length == 0) 
  1293.                         messageName = operation.Name;
  1294.                     if (messageName == operationName) 
  1295.                         operationExists = true;
  1296.                     if (messageName == null)
  1297.                         messageName = String.Empty;
  1298.                     methodsTable[messageName] = operation;
  1299.                 }
  1300.             }
  1301.         }
  1302.  
  1303.         MethodList.DataSource = methodsTable;
  1304.  
  1305.         // Databind all values within the page
  1306.     
  1307.         Page.DataBind();
  1308.     }
  1309.  
  1310.   </script>
  1311.  
  1312.   <head>
  1313.  
  1314.     <link rel="alternate" type="text/xml" href="<%#FileName%>?disco"/>
  1315.  
  1316.     <style type="text/css">
  1317.     
  1318.         BODY { <%#GetLocalizedText("StyleBODY")%> }
  1319.         #content { <%#GetLocalizedText("Stylecontent")%> }
  1320.         A:link { <%#GetLocalizedText("StyleAlink")%> }
  1321.         A:visited { <%#GetLocalizedText("StyleAvisited")%> }
  1322.         A:active { <%#GetLocalizedText("StyleAactive")%> }
  1323.         A:hover { <%#GetLocalizedText("StyleAhover")%> }
  1324.         P { <%#GetLocalizedText("StyleP")%> }
  1325.         pre { <%#GetLocalizedText("Stylepre")%> }
  1326.         td { <%#GetLocalizedText("Styletd")%> }
  1327.         h2 { <%#GetLocalizedText("Styleh2")%> }
  1328.         h3 { <%#GetLocalizedText("Styleh3")%> }
  1329.         ul { <%#GetLocalizedText("Styleul")%> }
  1330.         ol { <%#GetLocalizedText("Styleol")%> }
  1331.         li { <%#GetLocalizedText("Styleli")%> }
  1332.         font.value { <%#GetLocalizedText("Stylefontvalue")%> }
  1333.         font.key { <%#GetLocalizedText("Stylefontkey")%> }
  1334.         .heading1 { <%#GetLocalizedText("Styleheading1")%> }
  1335.         .button { <%#GetLocalizedText("Stylebutton")%> }
  1336.         .frmheader { <%#GetLocalizedText("Stylefrmheader")%> }
  1337.         .frmtext { <%#GetLocalizedText("Stylefrmtext")%> }
  1338.         .frmInput { <%#GetLocalizedText("StylefrmInput")%> }
  1339.         .intro { <%#GetLocalizedText("Styleintro")%> }
  1340.            
  1341.     </style>
  1342.  
  1343.     <title><%#ServiceName + " " + GetLocalizedText("WebService")%></title>
  1344.  
  1345.   </head>
  1346.  
  1347.   <body>
  1348.  
  1349.     <div id="content">
  1350.  
  1351.       <p class="heading1"><%#ServiceName%></p><br>
  1352.  
  1353.       <span visible='<%#ShowingMethodList && ServiceDocumentation.Length > 0%>' runat=server>
  1354.           <p class="intro"><%#ServiceDocumentation%></p>
  1355.       </span>
  1356.  
  1357.       <span visible='<%#ShowingMethodList%>' runat=server>
  1358.  
  1359.           <p class="intro"><%#GetLocalizedText("OperationsIntro", new object[] { EscapedFileName + "?WSDL" })%></p>
  1360.  
  1361.           <asp:repeater id="MethodList" runat=server>
  1362.       
  1363.             <headertemplate name="headertemplate">
  1364.               <ul>
  1365.             </headertemplate>
  1366.       
  1367.             <itemtemplate name="itemtemplate">
  1368.               <li>
  1369.                 <a href="<%#EscapedFileName%>?op=<%#EscapeParam(DataBinder.Eval(Container.DataItem, "Key").ToString())%>"><%#DataBinder.Eval(Container.DataItem, "Key")%></a>
  1370.                 <span visible='<%#((string)DataBinder.Eval(Container.DataItem, "Value.Documentation")).Length>0%>' runat=server>
  1371.                   <br><%#DataBinder.Eval(Container.DataItem, "Value.Documentation")%>
  1372.                 </span>
  1373.               </li>
  1374.               <p>
  1375.             </itemtemplate>
  1376.       
  1377.             <footertemplate name="footertemplate">
  1378.               </ul>
  1379.             </footertemplate>
  1380.       
  1381.           </asp:repeater>
  1382.       </span>
  1383.  
  1384.       <span visible='<%#!ShowingMethodList && OperationExists%>' runat=server>
  1385.           <p class="intro"><%#GetLocalizedText("LinkBack", new object[] { EscapedFileName })%></p>
  1386.           <h2><%#OperationName%></h2>
  1387.           <p class="intro"><%#SoapOperationBinding == null ? "" : SoapOperation.Documentation%></p>
  1388.  
  1389.           <h3><%#GetLocalizedText("TestHeader")%></h3>
  1390.           
  1391.           <% if (!showPost) { 
  1392.                  if (!ShowingHttpGet) { %>
  1393.                      <%#GetLocalizedText("NoHttpGetTest")%>
  1394.               <% }
  1395.                  else {
  1396.                      if (!ShowGetTestForm) { %>
  1397.                         <%#GetLocalizedText("NoTestNonPrimitive")%>
  1398.                   <% }
  1399.                      else { %>
  1400.  
  1401.                       <%#GetLocalizedText("TestText")%>
  1402.  
  1403.                       <form target="_blank" action='<%#TryGetUrl == null ? "" : TryGetUrl.AbsoluteUri%>' method="GET">
  1404.                         <asp:repeater datasource='<%#TryGetMessageParts%>' runat=server>
  1405.  
  1406.                         <headertemplate name="HeaderTemplate">
  1407.                            <table cellspacing="0" cellpadding="4" frame="box" bordercolor="#dcdcdc" rules="none" style="border-collapse: collapse;">
  1408.                              <tr visible='<%# TryGetMessageParts.Length > 0%>' runat=server>
  1409.                             <td class="frmHeader" background="#dcdcdc" style="border-right: 2px solid white;"><%#GetLocalizedText("Parameter")%></td>
  1410.                             <td class="frmHeader" background="#dcdcdc"><%#GetLocalizedText("Value")%></td>
  1411.                           </tr>
  1412.                         </headertemplate>
  1413.  
  1414.                         <itemtemplate name="ItemTemplate">
  1415.                           <tr>
  1416.                             <td class="frmText" style="color: #000000; font-weight:normal;"><%# ((MessagePart)Container.DataItem).Name %>:</td>
  1417.                             <td><input class="frmInput" type="text" size="50" name="<%# ((MessagePart)Container.DataItem).Name %>"></td>
  1418.                           </tr>
  1419.                         </itemtemplate>
  1420.  
  1421.                         <footertemplate name="FooterTemplate">
  1422.                           <tr>
  1423.                             <td></td>
  1424.                             <td align="right"> <input type="submit" value="<%#GetLocalizedText("InvokeButton")%>" class="button"></td>
  1425.                           </tr>
  1426.                           </table>
  1427.                         </footertemplate>
  1428.                     </asp:repeater>
  1429.  
  1430.                       </form>
  1431.                   <% } 
  1432.                  }
  1433.              }
  1434.              else { // showPost
  1435.                  if (!ShowingHttpPost) { 
  1436.                     if (requestIsLocal) { %>
  1437.                         <%#GetLocalizedText("NoTestNonPrimitive")%>
  1438.                  <% }
  1439.                     else { %>
  1440.                         <%#GetLocalizedText("NoTestFormRemote")%>
  1441.                  <% }
  1442.                  }
  1443.                  else {
  1444.                      if (!ShowPostTestForm) { %>
  1445.                         <%#GetLocalizedText("NoTestNonPrimitive")%>
  1446.                      
  1447.                   <% }
  1448.                      else { %>                      
  1449.  
  1450.                     
  1451.                       <%#GetLocalizedText("TestText")%>
  1452.  
  1453.  
  1454.  
  1455.                       <form target="_blank" action='<%#TryPostUrl == null ? "" : TryPostUrl.AbsoluteUri%>' method="POST">                      
  1456.                         <asp:repeater datasource='<%#TryPostMessageParts%>' runat=server>
  1457.  
  1458.                         <headertemplate name="HeaderTemplate">
  1459.                           <table cellspacing="0" cellpadding="4" frame="box" bordercolor="#dcdcdc" rules="none" style="border-collapse: collapse;">
  1460.                           <tr visible='<%# TryPostMessageParts.Length > 0%>' runat=server>
  1461.                             <td class="frmHeader" background="#dcdcdc" style="border-right: 2px solid white;"><%#GetLocalizedText("Parameter")%></td>
  1462.                             <td class="frmHeader" background="#dcdcdc"><%#GetLocalizedText("Value")%></td>
  1463.                           </tr>
  1464.                         </headertemplate>
  1465.  
  1466.                         <itemtemplate name="ItemTemplate">
  1467.                           <tr>
  1468.                             <td class="frmText" style="color: #000000; font-weight: normal;"><%# ((MessagePart)Container.DataItem).Name %>:</td>
  1469.                             <td><input class="frmInput" type="text" size="50" name="<%# ((MessagePart)Container.DataItem).Name %>"></td>
  1470.                           </tr>
  1471.                         </itemtemplate>                        
  1472.                         
  1473.                         <footertemplate name="FooterTemplate">
  1474.                         <tr>
  1475.                           <td></td>
  1476.                           <td align="right"> <input type="submit" value="<%#GetLocalizedText("InvokeButton")%>" class="button"></td>
  1477.                         </tr>
  1478.                         </table>
  1479.                       </footertemplate>
  1480.                     </asp:repeater>
  1481.  
  1482.                     </form>
  1483.                   <% }
  1484.                  }
  1485.              } %>
  1486.           <span visible='<%#ShowingSoap%>' runat=server>
  1487.               <h3><%#GetLocalizedText("SoapTitle")%></h3>
  1488.               <p><%#GetLocalizedText("SoapText")%></p>
  1489.  
  1490.               <pre><%#SoapOperationInput%></pre>
  1491.  
  1492.               <pre><%#SoapOperationOutput%></pre>
  1493.           </span>
  1494.  
  1495.           <span visible='<%#ShowingHttpGet%>' runat=server>
  1496.               <h3><%#GetLocalizedText("HttpGetTitle")%></h3>
  1497.               <p><%#GetLocalizedText("HttpGetText")%></p>
  1498.  
  1499.               <pre><%#HttpGetOperationInput%></pre>
  1500.  
  1501.               <pre><%#HttpGetOperationOutput%></pre>
  1502.           </span>
  1503.  
  1504.           <span visible='<%#ShowingHttpPost%>' runat=server>
  1505.               <h3><%#GetLocalizedText("HttpPostTitle")%></h3>
  1506.               <p><%#GetLocalizedText("HttpPostText")%></p>
  1507.  
  1508.               <pre><%#HttpPostOperationInput%></pre>
  1509.  
  1510.               <pre><%#HttpPostOperationOutput%></pre>
  1511.           </span>
  1512.  
  1513.       </span>
  1514.       <span visible='<%#ShowingMethodList && ServiceNamespace == "http://tempuri.org/"%>' runat=server>
  1515.           <hr>
  1516.           <h3><%#GetLocalizedText("DefaultNamespaceWarning1")%></h3>
  1517.           <h3><%#GetLocalizedText("DefaultNamespaceWarning2")%></h3>
  1518.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp1")%></p>
  1519.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp2")%></p>
  1520.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp3")%></p>
  1521.           <p class="intro">C#</p>
  1522.           <pre>[WebService(Namespace="http://microsoft.com/webservices/")]
  1523. public class MyWebService {
  1524.     // <%#GetLocalizedText("Implementation")%>
  1525. }</pre>
  1526.           <p class="intro">Visual Basic.NET</p>
  1527.           <pre><WebService(Namespace:="http://microsoft.com/webservices/")> Public Class MyWebService
  1528.     ' <%#GetLocalizedText("Implementation")%>
  1529. End Class</pre>
  1530.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp4")%></p>
  1531.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp5")%></p>
  1532.           <p class="intro"><%#GetLocalizedText("DefaultNamespaceHelp6")%></p>
  1533.       </span>
  1534.  
  1535.       <span visible='<%#!ShowingMethodList && !OperationExists%>' runat=server>
  1536.           <%#GetLocalizedText("LinkBack", new object[] { EscapedFileName })%>
  1537.           <h2><%#GetLocalizedText("MethodNotFound")%></h2>
  1538.           <%#GetLocalizedText("MethodNotFoundText", new object[] { Server.HtmlEncode(OperationName), ServiceName })%>
  1539.       </span>
  1540.  
  1541.  
  1542.   </body>
  1543. </html>
  1544.